/**
 * Licensed to the Apache Software Foundation （ASF） under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * （the "License"）； you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * https://www.q3z3.com
 * QQ : 939313737
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.platform.modules.sys.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.platform.common.enums.UserTypeEnum;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.common.web.utils.Pager;
import com.platform.common.exception.BaseException;
import com.platform.common.shiro.ShiroAuthorizingRealm;
import com.platform.modules.sys.dao.SysUserDao;
import com.platform.modules.sys.domain.SysUser;
import com.platform.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 系统用户表 服务层实现
 * </p>
 */
@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(sysUserDao);
    }

    @Override
    public SysUser queryByUsername(String username) {
        return sysUserDao.queryByUsername(username);
    }

    @Override
    public void updPassword(SysUser sysUser) {
        sysUserDao.updPassword(sysUser.getId(), sysUser.getPassword());
    }

    @Override
    public void reset(SysUser sysUser) {
        Long id = sysUser.getId();
        String password = sysUser.getPassword();
        SysUser user = super.getById(id);
        if (user == null) {
            throw new BaseException("账号不存在");
        }
        String salt = user.getSalt();
        sysUserDao.updPassword(id, ShiroAuthorizingRealm.md5(password, salt));
    }

    @Override
    public Integer uniqueUsername(SysUser sysUser) {
        return sysUserDao.uniqueUsername(sysUser);
    }

    @Override
    public Integer addSysUser(SysUser sysUser) {
        String salt = RandomUtil.randomString(4);
        sysUser.setSalt(salt);
        sysUser.setPassword(ShiroAuthorizingRealm.md5(sysUser.getPassword(), salt));
        sysUser.setUserType(UserTypeEnum.USER.getCode());
        return super.add(sysUser);
    }

    @Override
    public Integer editSysUser(SysUser sysUser) {
        sysUser.setUserType(UserTypeEnum.USER.getCode());
        return super.updateById(sysUser);
    }

    @Override
    public Long queryByRoleId(Long roleId) {
        SysUser sysUser = new SysUser();
        sysUser.setRoleId(roleId);
        return queryCount(sysUser);
    }

    @Override
    public List<SysUser> queryList(SysUser t) {
        t.setUserType(UserTypeEnum.USER.getCode());
        List<SysUser> dataList = sysUserDao.queryList(t);
        return dataList;
    }

    @Override
    public Pager<SysUser> queryPager(SysUser t) {
        List<SysUser> dataList = queryList(t);
        return formatPager(dataList);
    }

}
